package com.ruoyi.web.controller.system;

import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.util.ServletUtils;
import com.ruoyi.framework.web.base.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 登录验证
 *
 * @author ruoyi
 */
@Controller
@Api(tags = "登陆接口")
public class SysLoginController extends BaseController {

    @ApiOperation("to登录页面")
    @GetMapping("/login")
    public String login(HttpServletRequest request, HttpServletResponse response) {
        // 如果是Ajax请求，返回Json字符串。
        if (ServletUtils.isAjaxRequest(request)) {
            return ServletUtils.renderString(response, "{\"code\":\"1\",\"msg\":\"未登录或登录超时。请重新登录\"}");
        }

        return "login";
    }

    @ApiOperation(value = "登录", notes = "基于配置shiro.user.captchaEnabled来判断是否需要验证码登陆")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "登录账号", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "password", value = "登陆密码", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "rememberMe", value = "是否记住", dataType = "Boolean", paramType = "query", required = false) })
    @PostMapping("/login")
    @ResponseBody
    public AjaxResult ajaxLogin(String username, String password, Boolean rememberMe) {
        return login(username, password, rememberMe);
    }

    /**
     * 无验证码登录(供管理C端调用)
     *
     * @param
     * @return
     * @author chenck
     * @date 2018/12/26 22:53
     */
    @ApiOperation(value = "登录", notes = "无验证码登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "登录账号", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "password", value = "登陆密码", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "rememberMe", value = "是否记住", dataType = "Boolean", paramType = "query", required = false) })
    @PostMapping("/login/noCaptcha")
    @ResponseBody
    public AjaxResult ajaxLoginNoCaptcha(String username, String password, Boolean rememberMe) {
        return login(username, password, rememberMe);
    }

    /**
     * 登录
     *
     * @param
     * @return
     * @author chenck
     * @date 2018/12/26 23:13
     */
    public AjaxResult login(String username, String password, Boolean rememberMe) {
        UsernamePasswordToken token = new UsernamePasswordToken(username, password, rememberMe);
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);
            return success();
        } catch (AuthenticationException e) {
            String msg = "用户或密码错误";
            if (StringUtils.isNotEmpty(e.getMessage())) {
                msg = e.getMessage();
            }
            return error(msg);
        }
    }

    @ApiOperation(value = "to没有访问权限页面")
    @GetMapping("/unauth")
    public String unauth() {
        return "/error/unauth";
    }
}
